package com.juyu.miliao.api.controller;

import com.juyu.miliao.api.domain.Appkey;
import com.juyu.miliao.api.domain.dto.DynamicCommentInfoDTO;
import com.juyu.miliao.api.domain.dto.WhitePacketDynamicCommentInfoDTO;
import com.juyu.miliao.api.service.AppKeyService;
import com.juyu.miliao.api.service.DynamicCommentInfoService;
import com.juyu.miliao.api.service.OpenApiService;
import com.juyu.miliao.api.service.whitepacket.WhitePacketDynamicCommentInfoService;
import com.juyu.miliao.common.domain.CommonHead;
import com.juyu.miliao.common.domain.OpenApiStatus;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import com.juyu.miliao.common.util.VerifyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author xuanqicheng
 * @create 2019-21-12:51
 */
@RestController
@RequestMapping("/dynamicComment")
public class DynamicCommentInfoController {

    @Autowired
    private DynamicCommentInfoService dynamicCommentInfoService;

    @Autowired
    private WhitePacketDynamicCommentInfoService whitePacketDynamicCommentInfoService;

    @Autowired
    private OpenApiService openApiService;

    @Autowired
    private AppKeyService appKeyService;
    /**
     * 添加评论
     */
    @PostMapping("/")
    public void addDynamicComment(HttpServletRequest request, Authentication token,
                                  @Validated DynamicCommentInfoDTO dynamicCommentInfoDTO,
                                  BindingResult bindingResult) throws IOException {
        if(bindingResult.hasErrors()){
            throw  new ConsumeRuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }

        Subject subject = (Subject) token.getPrincipal();

        Boolean bl = getAudit(request);
        if(!bl){
            //检查用户是否达到发送评论限制
            Appkey appkey =appKeyService.getAppKeyByKey("dynamicCommentNumber");
            dynamicCommentInfoService.checkNumber(subject.getId(),Integer.parseInt(appkey.getValue()));

            dynamicCommentInfoDTO.setFromUserId(subject.getId());

            dynamicCommentInfoService.addDynamicComment(dynamicCommentInfoDTO);
        }else{
            WhitePacketDynamicCommentInfoDTO whitePacketDynamicCommentInfoDTO = new WhitePacketDynamicCommentInfoDTO();
            BeanUtils.copyProperties(dynamicCommentInfoDTO,whitePacketDynamicCommentInfoDTO);
            whitePacketDynamicCommentInfoDTO.setFromUserId(subject.getId());
            whitePacketDynamicCommentInfoService.addDynamicComment(whitePacketDynamicCommentInfoDTO);
        }


    }

    /**
     * 获取评论与回复的详细信息
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Object getDetailId(HttpServletRequest request,@PathVariable("id") long id){
        Boolean bl = getAudit(request);
        if(!bl) {
            return dynamicCommentInfoService.getDetailInfo(id);
        }else{
            return whitePacketDynamicCommentInfoService.getDetailInfo(id);
        }
    }

    @GetMapping("/{dynamicId}/{page}-{pageSize}")
    public Object getDynamicInfoList(HttpServletRequest request,@PathVariable("dynamicId") long dynamicId,@PathVariable("page") int page,
                                                  @PathVariable("pageSize") int pageSize) {

        if (page <= 0) {
            page = 1;
        }
        if (pageSize <= 0) {
            pageSize = 20;
        }

        Boolean bl = getAudit(request);
        if(!bl){
            Object obj = dynamicCommentInfoService.getDynamicCommentPageByDynamicId(dynamicId,page,pageSize);
            return obj;
        }else{
            Object obj = whitePacketDynamicCommentInfoService.getDynamicCommentPageByDynamicId(dynamicId,page,pageSize);

            return obj;
        }

    }


    /**
     * 获取审核状态
     * @param request
     * @return
     */
    private Boolean getAudit(HttpServletRequest request) {
        CommonHead commonHead = VerifyUtil.verifyCommonHead(request);
        OpenApiStatus ops = openApiService.getOpenApi(commonHead);
        if(ops != null && ops.getForwardAudit() && ops.getBackAudit()){
            return true;
        }else{
            return false;
        }
    }
}
